package com.zmzncs.lmtc.common.util;

import com.alibaba.fastjson.JSONObject;
import com.zmzncs.lmtc.common.config.ClientConfig;
import com.zmzncs.lmtc.common.pojo.tengda.CarOutPayForm;
import com.zmzncs.lmtc.common.pojo.tengda.TengdaForm;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;


@Slf4j
@Component
public class TengdaUtil {

    @Autowired
    private ClientConfig clientConfig;


    /**
     * 获取通道列表
     */
    public void channels(String parkCode, String uuid){
        List<Object> remoteDataItems = new ArrayList<>();
        JSONObject remoteBody = new JSONObject();
        remoteBody.put("code", 1);
        remoteBody.put("parkCode", parkCode);
        remoteBody.put("action", "all");
        remoteBody.put("opt", "admin");
        remoteBody.put("serviceId", "manage.channels");
        remoteBody.put("uuid", uuid);
        remoteBody.put("dataItems",remoteDataItems);

        clientConfig.publish("tcp/" + parkCode, "manage.channels", remoteBody.toString());
    }


    /**
     *平台控制进出场(开闸)
     * @param parkCode 停车场编号
     * @param channelId 通道ID
     * @param plateID 流水唯一标识
     * @param operator 抬闸管理员名称
     */
    public void parkingRemote(String parkCode,String channelId,String plateID,String operator) {

        JSONObject remoteParam = new JSONObject();
        remoteParam.put("channelId", channelId);
        remoteParam.put("cmd", 0);  //  命令: 0:控制进出 场；1:控制手动抬 闸
        remoteParam.put("plateID", plateID);    //  流水唯一标识：控制入场时对 应预入场的 recordNo; 控制出场时对 应预出场的 plateID 控制手动抬闸 时对应预上报 的plateId
        remoteParam.put("type", 0);    //  操作（对应入 场和出场）: 0:确定放行 1:取消放行 2:免费/异常 放行
        remoteParam.put("operator", operator);    //  0-非正常用户 1-正常用户
        remoteParam.put("openGateCase", 1);    //  放行原因，抬 闸、免费/异 常放行原因， 手动抬闸或免 费/异常放行 时必须 1:控制手动抬 闸

        List<Object> remoteDataItems = new ArrayList<>();
        remoteDataItems.add(remoteParam);

        JSONObject remoteBody = new JSONObject();
        remoteBody.put("code", 1);
        remoteBody.put("parkCode", parkCode);
        remoteBody.put("serviceId", "parking.remote");
        remoteBody.put("uuid", UUID.randomUUID().toString());
        remoteBody.put("dataItems", remoteDataItems);

        clientConfig.publish("tcp/" + parkCode, "parking.remote", remoteBody.toString());
    }

    /**
     * 获取车辆类型
     */
    public void getCarTypes(String parkCode, String uuid){
        List<Object> remoteDataItems = new ArrayList<>();
        JSONObject remoteBody = new JSONObject();
        remoteBody.put("code", 1);
        remoteBody.put("parkCode", parkCode);
        remoteBody.put("action", "get");
        remoteBody.put("opt", "admin");
        remoteBody.put("serviceId", "manage.cartypes");
        remoteBody.put("uuid", uuid);
        remoteBody.put("dataItems",remoteDataItems);

        clientConfig.publish("tcp/" + parkCode, "manage.cartypes", remoteBody.toString());
    }



    /**
     * 分页查询所有车辆
     * @param parkCode 停车场编号
     * @param page 当前页
     * @param pageSize 每页记录数
     * @param carNum 车位号码(为null则查询车场所有信息)
     */
    public void queryCars(String parkCode,Integer page,Integer pageSize,String carNum){
        JSONObject remoteParam = new JSONObject();
        remoteParam.put("page", page);
        remoteParam.put("pageSize", pageSize);
        if (carNum != null){
            remoteParam.put("carNum", carNum);
        }
        List<Object> remoteDataItems = new ArrayList<>();
        remoteDataItems.add(remoteParam);

        JSONObject remoteBody = new JSONObject();
        remoteBody.put("code", 1);
        remoteBody.put("parkCode", parkCode);
        remoteBody.put("action", "query");
        remoteBody.put("opt", "admin");
        remoteBody.put("serviceId", "manage.cars");
        remoteBody.put("uuid", UUID.randomUUID().toString());
        remoteBody.put("dataItems", remoteDataItems);

        clientConfig.publish("tcp/" + parkCode, "manage.cars", remoteBody.toString());
    }

    /**
     * 添加车辆
     * @param parkCode 车场编号
     * @param carNum 车牌号
     * @param carTypeCode 车辆类型编码
     * @param carMMoney 月租金额
     * @param timeStart 开始时间(yyyy-MM-dd HH:mm:ss)
     * @param timeEnd 结束时间(yyyy-MM-dd HH:mm:ss)
     */
    public void addCars(String parkCode,String carNum,Integer carTypeCode,Double carMMoney, String timeStart,String timeEnd, String uuid){
        JSONObject remoteParam = new JSONObject();
        remoteParam.put("CarNum", carNum);
        remoteParam.put("CarTypeCode", carTypeCode);
        remoteParam.put("CarMMoney", carMMoney);
        remoteParam.put("TimeStart", timeStart);
        remoteParam.put("TimeEnd", timeEnd);
        remoteParam.put("State", 1);

        List<Object> remoteDataItems = new ArrayList<>();
        remoteDataItems.add(remoteParam);

        JSONObject remoteBody = new JSONObject();
        remoteBody.put("code", 1);
        remoteBody.put("parkCode", parkCode);
        remoteBody.put("action", "add");
        remoteBody.put("opt", "admin");
        remoteBody.put("serviceId", "manage.cars");
        remoteBody.put("uuid", uuid);
        remoteBody.put("dataItems", remoteDataItems);

        clientConfig.publish("tcp/" + parkCode, "manage.cars", remoteBody.toString());
    }

    /**
     * 修改车辆信息
     * @param parkCode 车场编号
     * @param carNum 车牌号
     * @param carTypeCode 车辆类型编码
     * @param carMMoney 月租金额
     * @param timeStart 开始时间(yyyy-MM-dd HH:mm:ss)
     * @param timeEnd 结束时间(yyyy-MM-dd HH:mm:ss)
     */
    public void editCars(String uuId,String parkCode,String carNum,Integer carTypeCode,Double carMMoney, String timeStart,String timeEnd){
        JSONObject remoteParam = new JSONObject();
        remoteParam.put("UUID", uuId);
        remoteParam.put("CarNum", carNum);
        remoteParam.put("CarTypeCode", carTypeCode);
        remoteParam.put("CarMMoney", carMMoney);
        remoteParam.put("TimeStart", timeStart);
        remoteParam.put("TimeEnd", timeEnd);

        List<Object> remoteDataItems = new ArrayList<>();
        remoteDataItems.add(remoteParam);

        JSONObject remoteBody = new JSONObject();
        remoteBody.put("code", 1);
        remoteBody.put("parkCode", parkCode);
        remoteBody.put("action", "edit");
        remoteBody.put("opt", "admin");
        remoteBody.put("serviceId", "manage.cars");
        remoteBody.put("uuid", uuId);
        remoteBody.put("dataItems", remoteDataItems);

        clientConfig.publish("tcp/" + parkCode, "manage.cars", remoteBody.toString());
    }

    /**
     * 车牌月租
     */
    public void monthCard(String parkCode, String carNum, String uuid){
        JSONObject monthCardParam = new JSONObject();
        monthCardParam.put("carNum", carNum);
        monthCardParam.put("page", 0);
        monthCardParam.put("pageSize", 100);

        List<Object> dataItems = new ArrayList<>();
        dataItems.add(monthCardParam);

        JSONObject monthCardBody = new JSONObject();
        monthCardBody.put("code", 1);
        monthCardBody.put("parkCode", parkCode);
        monthCardBody.put("action", "query");
        monthCardBody.put("serviceId", "manage.cars");
        monthCardBody.put("uuid", uuid);
        monthCardBody.put("dataItems", dataItems);

        clientConfig.publish("tcp/" + parkCode, "manage.cars", monthCardBody.toString());
    }

    /**
     * 开闸落闸
     */
    public void gateOnOff(String bolianParkId, String channelId, String action, String opt, String uuid){
        JSONObject monthCardParam = new JSONObject();
        monthCardParam.put("channelId", channelId);

        List<Object> dataItems = new ArrayList<>();
        dataItems.add(monthCardParam);

        JSONObject monthCardBody = new JSONObject();
        monthCardBody.put("code", 1);
        monthCardBody.put("parkCode", bolianParkId);
        monthCardBody.put("action", action);
        monthCardBody.put("opt", opt);
        monthCardBody.put("serviceId", "manage.channels");
        monthCardBody.put("uuid", uuid);
        monthCardBody.put("dataItems", dataItems);

        clientConfig.publish("tcp/" + bolianParkId, "manage.channels", monthCardBody.toString());
    }

    /**
     * 支付通知
     */
    public void payParkingFeeOrder(TengdaForm tengdaForm, CarOutPayForm outPayForm){
        JSONObject payNoticeParam = new JSONObject();
        List<Object> payNoticeDataItems = new ArrayList<>();
        String orderNo = UUID.randomUUID().toString();
        payNoticeParam.put("orderNo", orderNo);
        payNoticeParam.put("paidFee", outPayForm.getPaidFee());
        payNoticeParam.put("payTime", DateTimeUtil.getTime());
        payNoticeParam.put("enterTime", outPayForm.getEnterTime());
        payNoticeParam.put("payType", 3); //  1 微信 2 支付宝 3 平台余额 4 现金 5 银联 6 其他 10 平台停车券 11 积分兑换
        payNoticeParam.put("payAuth",  UUID.randomUUID().toString());
        payNoticeParam.put("payAuthName", "中萌智能");
        payNoticeParam.put("payPath", 2000);
        payNoticeParam.put("payPathDes", "中萌支付");
//        payNoticeParam.put("payPathExp", null);  //  可空，对缴费渠道做进一步描述;
        payNoticeParam.put("payChannel", 2);  //  1-车场收费 2.云平台收费 3-第三方收费 33-出场扫码支付
        payNoticeParam.put("outChannelid", outPayForm.getOutChannelid());
        payNoticeParam.put("recordNo", outPayForm.getRecordNo());
        payNoticeParam.put("plateNumber", outPayForm.getPlateNumber());
        payNoticeDataItems.add(payNoticeParam);

        JSONObject payNoticeBody = new JSONObject();
        payNoticeBody.put("code", 1);
        payNoticeBody.put("parkCode", tengdaForm.getParkCode());
        payNoticeBody.put("serviceId", "pay.payparkingfeeorder");
        payNoticeBody.put("uuid", tengdaForm.getUuid());
        payNoticeBody.put("dataItems", payNoticeDataItems);
        clientConfig.publish("tcp/" + tengdaForm.getParkCode(), "pay.payparkingfeeorder", payNoticeBody.toString());
    }

    /**
     * 开闸
     */
    public void parkingRemote(TengdaForm tengdaForm, CarOutPayForm outPayForm){
//        JSONObject json = JSONObject.parseObject(form.getDataItems().get(0).toJSONString());
        /*开闸*/
        JSONObject remoteParam = new JSONObject();
        remoteParam.put("channelId", outPayForm.getOutChannelid());
        remoteParam.put("cmd", 0);  //  命令: 0:控制进出 场；1:控制手动抬 闸
        remoteParam.put("plateID", outPayForm.getPlateID());    //  流水唯一标 识：控制入场时对 应预入场的 recordNo; 控制出场时对 应预出场的 plateID 控制手动抬闸 时对应预上报 的plateId
        remoteParam.put("type", 0);    //  操作（对应入 场和出场）: 0:确定放行 1:取消放行 2:免费/异常 放行
        remoteParam.put("operator", tengdaForm.getOpt());    //  0-非正常用户 1-正常用户
        remoteParam.put("openGateCase", 1);    //  放行原因，抬 闸、免费/异 常放行原因， 手动抬闸或免 费/异常放行 时必须 1:控制手动抬 闸

        List<Object> remoteDataItems = new ArrayList<>();
        remoteDataItems.add(remoteParam);

        JSONObject remoteBody = new JSONObject();
        remoteBody.put("code", 1);
        remoteBody.put("parkCode", tengdaForm.getParkCode());
        remoteBody.put("serviceId", "parking.remote");
        remoteBody.put("uuid", tengdaForm.getUuid());
        remoteBody.put("dataItems", remoteDataItems);

        clientConfig.publish("tcp/" + tengdaForm.getParkCode(), "parking.remote", remoteBody.toString());
    }

}
